//日期格式转换
const formatDateTime = (date, format) => {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? '上午' : '下午', // 上午/下午
    A: date.getHours() < 12 ? 'AM' : 'PM', // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      );
    }
  }
  return format;
}

const getCookie = () => {
  let cc = document.cookie("bk_token")
}


/**
 * 智能抄表数据处理
 * data = {label:String,children:List}
 */
const doLableData = (data, type) => {
  let returnData = {};
  if (type === "all") {
    //开始处理第一层
    let one = {};
    let tow = {};
    data["label"] = data.name;
    data["children"] = data.regionVOList;
    let temp1 = [];
    for (let i = 0; i < data.children.length; i++) {
      //开始处理第二层
      data.children[i]["label"] = data.children[i].regionName;
      data.children[i]["children"] = data.children[i].deviceTypeVO;
      for (let j = 0; j < data.children[i].children; j++) {
        data.children[i].children[j]["label"] = data.children[i].children[j].deviceTypeName
        data.children[i].children[j]["children"] = data.children[i].children[j].deviceList
        for (let n = 0; n < data.children[i].children[j].children.length; n++) {
          console.log(data.children[i].children[j].children[n])
          data.children[i].children[j].children[n]["label"] = data.children[i].children[j].children[n].deviceTypeName;
          data.children[i].children[j].children[n]["id"] = data.children[i].children[j].children[n].deviceTypeId;
        }
      }
    }


  }
  if (type === "one") {
    for (let i = 0; i < data.length; i++) {

    }
  }
  return data;
}
const isObjectValueEqual = (a, b) => {
  // 判断两个对象是否指向同一内存，指向同一内存返回 true
  if (a === b) return true // 获取两个对象键值数组
  let aProps = Object.getOwnPropertyNames(a)
  let bProps = Object.getOwnPropertyNames(b)
  // 判断两个对象键值数组长度是否一致，不一致返回 false
  if (aProps.length !== bProps.length) return false // 遍历对象的键值
  for (let prop in a) {
    // 判断 a 的键值，在 b 中是否存在，不存在，返回 false
    if (b.hasOwnProperty(prop)) {
      // 判断 a 的键值是否为对象，是则递归，不是对象直接判断键值是否相等，不相等返回 false
      if (typeof a[prop] === 'object') {
        if (!isObjectValueEqual(a[prop], b[prop])) return false
      } else if (a[prop] !== b[prop]) {
        return false
      }
    } else {
      return false
    }
  }
  return true
}
//去除json空对象
const deleteEmptyProperty = (object) => {
  for (var i in object) {
    var value = object[i];
    if (typeof value === 'object') {
      if (Array.isArray(value)) {
        if (value.length == 0) {
          delete object[i];
          continue;
        }
      }
      deleteEmptyProperty(value);
      if (isEmpty(value)) {
        delete object[i];
      }
    } else {
      if (value === '' || value === null || value === undefined) {
        delete object[i];
      }
    }
  }
  return object;
}

const isEmpty = (object) => {
  for (var name in object) {
    return false;
  }
  return true;
}

/**
 * 数组去重
 * @param {*} tempArr 
 * @returns 
 */
const fn2=(arr)=> {
  let some = [];
  arr.forEach(el => {
    if (!some.some(e => e.flag == el.flag)) {
      some.push(el);
    }
  });
  return some;
};

export {
  formatDateTime,
  getCookie,
  doLableData,
  isObjectValueEqual,
  deleteEmptyProperty,
  fn2
}